import Foundation
import SwiftUI

struct Scenario: Identifiable, Hashable {
    let id = UUID()
    let code: String // 场景编号
    let title: String
    let subtitle: String
    let icon: String
    let minDurationSec: Double
    let maxDurationSec: Double
    let gradient: [Color]
    let category: ScenarioCategory
    
    var hasRange: Bool { minDurationSec != maxDurationSec }
    var defaultDurationSec: Double { (minDurationSec + maxDurationSec) / 2 }
}

enum ScenarioCategory: String, CaseIterable {
    case freeRecording = "自由记录"
    case sittingChewing = "静坐 + 咀嚼"
    case sittingNonChewing = "静坐 + 非咀嚼动作"
    case walkingOnly = "行走（单纯步态）"
    case walkingChewing = "行走 + 咀嚼"
    case walkingNonChewing = "行走 + 非咀嚼动作"
}

extension Scenario {
    static let all: [Scenario] = [
        // 0. 自由记录
        Scenario(code: "🆓", title: "自由记录", subtitle: "完整用餐记录", icon: "record.circle", minDurationSec: 0, maxDurationSec: 0, gradient: [.purple, .pink, .orange], category: .freeRecording),
        // 0.2 自由记录2（非咀嚼）
        Scenario(code: "🆓2", title: "自由记录2", subtitle: "仅非咀嚼记录（不限时）", icon: "record.circle", minDurationSec: 0, maxDurationSec: 0, gradient: [.purple, .pink, .orange], category: .freeRecording),
        
        // A. 静坐 + 咀嚼（4 类）
        Scenario(code: "①", title: "咀嚼（软）", subtitle: "面包、香蕉、熟土豆", icon: "fork.knife", minDurationSec: 30, maxDurationSec: 60, gradient: [.green, .teal], category: .sittingChewing),
        Scenario(code: "②", title: "咀嚼（脆）", subtitle: "薯片、黄瓜、饼干", icon: "sparkles", minDurationSec: 30, maxDurationSec: 60, gradient: [.yellow, .orange], category: .sittingChewing),
        Scenario(code: "③", title: "咀嚼（硬）", subtitle: "花生、胡萝卜、坚果", icon: "cube.fill", minDurationSec: 30, maxDurationSec: 60, gradient: [.red, .pink], category: .sittingChewing),
        Scenario(code: "④", title: "咀嚼（糊状）", subtitle: "米饭、粥、蛋糕糊", icon: "drop.fill", minDurationSec: 30, maxDurationSec: 60, gradient: [.purple, .indigo], category: .sittingChewing),
        
        // B. 静坐 + 非咀嚼动作（4 类）
        Scenario(code: "⑤", title: "说话", subtitle: "自然对话或朗读", icon: "bubble.left.and.bubble.right.fill", minDurationSec: 60, maxDurationSec: 120, gradient: [.cyan, .blue], category: .sittingNonChewing),
        Scenario(code: "⑥", title: "吞咽", subtitle: "喝水后刻意吞咽", icon: "drop.triangle.fill", minDurationSec: 30, maxDurationSec: 60, gradient: [.teal, .cyan], category: .sittingNonChewing),
        Scenario(code: "⑦", title: "喝水", subtitle: "一口一口喝", icon: "cup.and.saucer.fill", minDurationSec: 30, maxDurationSec: 60, gradient: [.blue, .indigo], category: .sittingNonChewing),
        Scenario(code: "⑧", title: "头部动作", subtitle: "点头/摇头/转头", icon: "arrow.up.and.down.and.arrow.left.and.right", minDurationSec: 30, maxDurationSec: 60, gradient: [.orange, .yellow], category: .sittingNonChewing),
        
        // C. 行走（单纯步态，无口部动作）
        Scenario(code: "⑨", title: "行走（慢）", subtitle: "缓步 2 分钟", icon: "figure.walk", minDurationSec: 120, maxDurationSec: 120, gradient: [.green, .mint], category: .walkingOnly),
        Scenario(code: "⑩", title: "行走（正常）", subtitle: "日常速度 2 分钟", icon: "figure.walk.motion", minDurationSec: 120, maxDurationSec: 120, gradient: [.blue, .cyan], category: .walkingOnly),
        Scenario(code: "⑪", title: "行走（快）", subtitle: "稍快步 1 分钟", icon: "figure.run", minDurationSec: 60, maxDurationSec: 60, gradient: [.red, .orange], category: .walkingOnly),
        
        // D. 行走 + 咀嚼（3 类主食物）
        Scenario(code: "⑫", title: "行走 + 咀嚼（软）", subtitle: "面包、香蕉", icon: "figure.walk", minDurationSec: 60, maxDurationSec: 120, gradient: [.green, .teal], category: .walkingChewing),
        Scenario(code: "⑬", title: "行走 + 咀嚼（脆）", subtitle: "薯片、饼干", icon: "figure.walk", minDurationSec: 60, maxDurationSec: 120, gradient: [.yellow, .orange], category: .walkingChewing),
        Scenario(code: "⑭", title: "行走 + 咀嚼（硬）", subtitle: "花生、坚果", icon: "figure.walk", minDurationSec: 60, maxDurationSec: 120, gradient: [.red, .pink], category: .walkingChewing),
        
        // E. 行走 + 非咀嚼动作（4 类）
        Scenario(code: "⑮", title: "行走 + 说话", subtitle: "自然交谈/朗读", icon: "figure.walk", minDurationSec: 60, maxDurationSec: 120, gradient: [.cyan, .blue], category: .walkingNonChewing),
        Scenario(code: "⑯", title: "行走 + 吞咽", subtitle: "喝水后轻吞咽", icon: "figure.walk", minDurationSec: 30, maxDurationSec: 60, gradient: [.teal, .cyan], category: .walkingNonChewing),
        Scenario(code: "⑰", title: "行走 + 喝水", subtitle: "小口多次", icon: "figure.walk", minDurationSec: 30, maxDurationSec: 60, gradient: [.blue, .indigo], category: .walkingNonChewing),
        Scenario(code: "⑱", title: "行走 + 头部动作", subtitle: "边走边点头/转头", icon: "figure.walk", minDurationSec: 30, maxDurationSec: 60, gradient: [.orange, .yellow], category: .walkingNonChewing)
    ]
    
    // 按类别分组
    static func groupedByCategory() -> [(category: ScenarioCategory, scenarios: [Scenario])] {
        let grouped = Dictionary(grouping: all, by: { $0.category })
        return ScenarioCategory.allCases.compactMap { category in
            guard let scenarios = grouped[category] else { return nil }
            return (category, scenarios)
        }
    }
}

struct Session: Identifiable, Hashable {
    let id = UUID()
    let scenario: Scenario
    let startDate: Date
    let endDate: Date
    let durationSec: Double
    var note: String?
}
